"use strict";

const Controller = require("egg").Controller;

class QuestionController extends Controller {
    /**
     * 问题列表
     */
    async index() {
        // 路径参数、查询参数
        const { tid } = this.ctx.query;
        // 读取数据
        const res = await this.service.question.readListByTid(tid);
        // 返回响应
        this.ctx.body = {
            code: 200,
            data: res,
            message: "请求成功",
        };
    }

    async types() {
        // 读取数据
        const res = await this.service.question.readTypeList();
        // 返回响应
        this.ctx.body = {
            code: 200,
            data: res,
            message: "请求成功",
        };
    }

    /**
     * 添加问题
     */
    async create() {
        const { title, content, tid } = this.ctx.request.body;
        const res = await this.service.question.add(title, content, tid);
        if (res.affectedRows === 1) {
            this.ctx.body = {
                code: 201,
                message: "添加成功",
            };
        }
    }
}

module.exports = QuestionController;
